package com.ruoyi.reservation.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.staff.domain.Staff;
import com.ruoyi.staff.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.reservation.mapper.ReservationMapper;
import com.ruoyi.reservation.domain.Reservation;
import com.ruoyi.reservation.service.IReservationService;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-19
 */
@Service
public class ReservationServiceImpl implements IReservationService 
{
    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private IStaffService staffService;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectReservationById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        // 判断角色
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        Long roleId = roles.get(0).getRoleId();
        Long userId = loginUser.getUserId();
        // 病人，只能查自己的预约信息
        if(roleId.equals(100L)){
            reservation.setUserId(userId);
        }
        // 医生，只能查自己的预约信息
        if(roleId.equals(101L)){
            Staff staff = staffService.selectStaffByUserId(userId);
            reservation.setStaffId(staff.getId());
        }
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        reservation.setCreateTime(DateUtils.getNowDate());
        // 新增预约，存入当前用户id（预约人）
        Long userId = SecurityUtils.getUserId();
        reservation.setUserId(userId);
        return reservationMapper.insertReservation(reservation);
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        reservation.setUpdateTime(DateUtils.getNowDate());
        return reservationMapper.updateReservation(reservation);
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteReservationByIds(ids);
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteReservationById(id);
    }

    @Override
    public List<Reservation> selectReservationAll() {
        return reservationMapper.selectReservationAll();
    }
}
